package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.ItemStatusDao;
import mil.af.amc.adpetracker.model.ItemStatus;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * Please fill in this Javadoc
 *
 * @author Jason Ferguson
 */
@Service("itemStatusDao")
public class HibernateItemStatusDao extends HibernateGenericDao<ItemStatus> implements ItemStatusDao {

    public HibernateItemStatusDao() {
        super(ItemStatus.class);
    }

    public ItemStatus getStatusByName(final String statusDescription) {

        return (ItemStatus) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {

                Criteria crit = session.createCriteria(ItemStatus.class);
                crit.add(Restrictions.eq("status", statusDescription.toUpperCase()));
                return crit.uniqueResult();

            }
        });
    }

}
